1 using System.Collections;
2 using
System.Collections.Generic;
3 using
UnityEngine;
4 using
UnityEngine.UI;
5
6 public
class CameraFollow : MonoBehaviour {
7
8     
public Transform target;
9     
public Transform leftBound;
10     
public Transform rightBound;
11     
public float speed = 5f;
12     
public bool isFollowing;
13
14
15     
private float arrowX;
16     
public bool allowToMove;
17
18     
private float dragSpeed = 0.05f;
19     
private float timeSinceShot;
20     
private Vector3 velocity = Vector3.zero;
21     
private Vector3 startPosition;
22     
private Vector3 camPos;
23
24     
// Use this for initialization
25     
void Start () {
26         startPosition = transform.localPosition;
27     }
28     
29     
// Update is called once per frame
30     
void Update () {
31         
if (GameplayController.instance.gameInProgress) {
32             
if (isFollowing) {
33                 
if (GameObject.FindGameObjectWithTag ("Player Bullet") != null) {
34                     MoveCameraFollow ();
35                 }
36             }
else {
37                 
if (!GameplayController.instance.player.GetChild (0).transform.GetComponent<Cannon> ().readyToShoot) {
38                     MoveCameraBackToStart ();
39                     AfterShotMoveAgain ();
40                     allowToMove =
false;
41                 }
else {
42                     timeSinceShot =
0;
43                     allowToMove =
true;
44                 }
45                 
46             }
47             
48             
if (Application.platform == RuntimePlatform.Android) {
49                 TouchMoveCamera ();
50             }
else if (Application.platform == RuntimePlatform.WindowsEditor) {
51                 MoveCamera ();
52             }
53         }
54
55     }
56
57     
void MoveCameraFollow(){
58         
if(target != null){
59             transform.position = Vector3.Lerp (
new Vector3 (transform.position.x, 0, -10f), new Vector3 (Mathf.Clamp (target.transform.position.x, leftBound.position.x, rightBound.position.x), 0, transform.position.z), Time.deltaTime * 10);
60         }
61     }
62
63     
void MoveCameraBackToStart(){
64         transform.position = Vector3.MoveTowards (transform.position, startPosition, Time.deltaTime *
5f);
65     }
66
67     
void AfterShotMoveAgain(){
68         timeSinceShot += Time.deltaTime;
69         
if (timeSinceShot > 2f) {
70             
if(startPosition == transform.position){
71                 GameplayController.instance.player.GetChild (
0).transform.GetComponent<Cannon> ().readyToShoot = true;
72             }
73         }
74     }
75
76     
void MoveCamera(){
77         arrowX = Input.GetAxis (
"Horizontal");
78
79         Transform cam = gameObject.transform;
80
81         
if (allowToMove) {
82             
if (arrowX != 0) {
83                 cam.position = cam.position + (
new Vector3 (arrowX, 0, 0) * speed * Time.deltaTime);
84                 
float camX = cam.position.x;
85                 camX = Mathf.Clamp (camX, leftBound.transform.position.x, rightBound.transform.position.x);
86                 cam.position =
new Vector3 (camX, cam.position.y, cam.position.z);
87             }
88         }
89     }
90
91     
void TouchMoveCamera(){
92         
if(allowToMove){
93
94             
/*if(Input.touchCount > 0){
95                 Touch touch = Input.GetTouch (
0);
96             
97                 Transform cam = gameObject.transform;
98
99
100                 
if(touch.phase == TouchPhase.Began){
101                     cam = touch.position;
102                 }
else if(touch.phase == TouchPhase.Moved){
103                     
104                 }
105             }*/

106
107             
if(Input.touchCount > 0){
108                 Touch touch = Input.GetTouch (
0);
109
110                 Transform cam = gameObject.transform;
111                 
if (touch.position.x > 300) {
112                     
if(touch.phase == TouchPhase.Began || touch.phase == TouchPhase.Stationary){
113                         camPos = touch.position;
114                     }
else if(touch.phase == TouchPhase.Moved){
115                         cam.position = cam.position + (
new Vector3 ((camPos.x - touch.position.x), 0, 0) * dragSpeed * Time.deltaTime);
116                         
float camX = cam.position.x;
117                         camX = Mathf.Clamp (camX, leftBound.transform.position.x, rightBound.transform.position.x);
118                         cam.position =
new Vector3 (camX, cam.position.y, cam.position.z);
119                     }
120                 }
121                     
122             }
123                 
124         }
125     }
126
127
128         
129 }


Use this for initialization

Update is called once per frame




trò chơi Cannon Siege miễn phí 12.310 lượt xem

Gõ tìm kiếm nhanh...